package com.ts.api.module.match.plan.internal.distribute.logger;

import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.constant.productflow.ProductApplyFlowStatusEnum;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeUserRequest;
import com.ts.api.module.product.entity.po.ProductApplyFlowPO;
import com.ts.api.module.product.entity.po.ProductPO;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import org.springframework.stereotype.Component;

/**
 *  分发流程日志器
 *
 *  @author Pan
 */
@Component
public class PlanDistributeFlowLogger {

    /**
     *
     *  构建下游撞库分发流程
     *
     *  @param  planDistributeUserRequest   计划分发用户传输对象
     *  @param  productPO                   主体实体
     *  @param  productPlanPO               计划实体
     *  @param  reqId                       当球请求id
     *  @return ProductApplyFlowPO
     */
    public ProductApplyFlowPO startDataExchangeFlow(PlanDistributeUserRequest planDistributeUserRequest,
                                                    ProductPO productPO, ProductPlanPO productPlanPO, String reqId) {
        //  撞库流程唯一id
        String flowUniqueId;

        //  如果上游渠道传递md5，否则使用请求id记录日志
        if (ValidParam.isNotEmpty(planDistributeUserRequest.getPhoneMd5())) {
            flowUniqueId = planDistributeUserRequest.getPhoneMd5();
        } else {
            flowUniqueId = reqId;
        }

        ProductApplyFlowPO productApplyFlowPO = new ProductApplyFlowPO();
        productApplyFlowPO.setLoanChannelId(planDistributeUserRequest.getLoanChannelId());
        productApplyFlowPO.setLoanProductId(productPO.getId());
        productApplyFlowPO.setLoanProductPlanId(productPlanPO.getId());
        productApplyFlowPO.setApiClientId(planDistributeUserRequest.getApiClientId());
        productApplyFlowPO.setPhoneMd5(flowUniqueId);
        productApplyFlowPO.setAreaName(planDistributeUserRequest.getCity());
        productApplyFlowPO.setAreaCode(planDistributeUserRequest.getAreaCode());
        productApplyFlowPO.setReqId(ClassConvertUtils.toLong(planDistributeUserRequest.getReqId()));
        productApplyFlowPO.setStatus(ProductApplyFlowStatusEnum.DATA_EXCHANGE_SUC.getType());
        productApplyFlowPO.setBizStatus("0");
        productApplyFlowPO.setCreateTime(DateUtils.currentTime());
        return productApplyFlowPO;
    }

}
